# -*- coding: utf-8 -*-

import datetime
import traceback
import time
from scrapy import log
from pymongo import MongoClient
from snowball_crawler.items import  SnowballCubeItem
from snowball_crawler.pipelines import mongodb


class snowballDailyPipeline(mongodb.mongodbPipeline):
    """
       save data to mongodb
    """

    def process_item(self, item, spider):
        # spider.logger.debug('SnowballTrading %s' % item['type'])
         if  isinstance(item, SnowballCubeItem):
            dic = item['cube_info']

            today = time.strftime("%Y-%m-%d", time.localtime())
            curr_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
            holding = {
                'owner_friends_count': dic['owner']['friends_count'],
                'owner_followers_count': dic['owner']['followers_count'],
                'owner_status_count': dic['owner']['status_count'],
                'holding': dic['last_success_rebalancing']['holdings'],
                'daily_gain': dic['daily_gain'],
                'total_gain': dic['total_gain'],
                'monthly_gain': dic['monthly_gain'],
                'net_value': dic['net_value'],
                'follower_count': dic['follower_count'],
                'update_time': today
            }
            info= {
                'symbol': dic['symbol'],
                'name': dic['name'],
                'description': dic['description'],
                'market': dic['market'],
                'owner_id': dic['owner_id'],
                'create_date': dic['created_date_format'],
                'active_flag': dic['active_flag'],
                'last_holding': holding,
                'last_holding_updatetime': curr_time,
                'daily_holding.%s'%today : holding # 每日持仓和收益统计
                #hist_rebalancing[] 调仓历史
            }

            #daily_holding={

            result= self.db['cubes'].update_one({'symbol': info['symbol']} ,{'$set': info} , True)
            log.msg("Item %s updated to MongoDB database %s/cube_info" % (result, self.MONGODB_DB), level=log.DEBUG,
                    spider=spider)

         return item
